package com.fsc.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fsc._enum.ReturnEnum;
import com.fsc.entity.ResultWrapper;
import com.fsc.entity.domain.AnnRepoNews;
import com.fsc.entity.domain.LoginInfo;
import com.fsc.entity.dto.AnnRepoNewsDTO;
import com.fsc.entity.dto.NewsDTO;
import com.fsc.entity.dto.NewsEditDTO;
import com.fsc.entity.vo.AnnRepoNewsVO;
import com.fsc.handler.BusinessException;
import com.fsc.handler.GlobalExceptionHandler;
import com.fsc.mapper.LoginInfoMapper;
import com.fsc.service.AnnRepoNewsService;
import com.fsc.mapper.AnnRepoNewsMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.ExceptionHandler;

import java.util.ArrayList;
import java.util.List;

import static com.fsc._enum.ReturnEnum.ANN_REPO_NEWS_NOT_EXIST;
import static com.fsc._enum.ReturnEnum.SUCCESS;

/**
* @author 86183
* @description 针对表【ann_repo_news(通知、公告、新闻表)】的数据库操作Service实现
* @createDate 2024-06-02 13:15:21
*/
@Service
public class AnnRepoNewsServiceImpl extends ServiceImpl<AnnRepoNewsMapper, AnnRepoNews>
    implements AnnRepoNewsService{
    @Autowired
    private AnnRepoNewsMapper annRepoNewsMapper;

    @Autowired
    private LoginInfoMapper loginInfoMapper;

    public ResultWrapper<List<AnnRepoNewsVO>> getNews(NewsDTO newsDTO){
        List<AnnRepoNews> list = annRepoNewsMapper.getNews(newsDTO);
        List<AnnRepoNewsVO> annRepoNewsVOList = new ArrayList<>();
        for (AnnRepoNews annRepoNews : list) {
            // 获取authorId
            int authorId = annRepoNews.getAuthorId();
            // 查询LoginInfo
            LoginInfo loginInfo = loginInfoMapper.selectLoginInfo(authorId);
            // 创建AnnRepoNewsVO对象
            AnnRepoNewsVO annRepoNewsVO = new AnnRepoNewsVO();
            // 复制AnnRepoNews的字段到AnnRepoNewsVO
            BeanUtils.copyProperties(annRepoNews, annRepoNewsVO);
            // 设置LoginInfo的字段到AnnRepoNewsVO
            if (loginInfo != null) {
                annRepoNewsVO.setUsername(loginInfo.getUsername());
                annRepoNewsVO.setIconUrl(loginInfo.getIconUrl());
            }
            // 添加到结果列表
            annRepoNewsVOList.add(annRepoNewsVO);
        }
//        System.out.println(annRepoNewsVOList);
        return ResultWrapper.success(SUCCESS,annRepoNewsVOList);
    }

    public ResultWrapper<List<AnnRepoNews>> getAllNews(NewsEditDTO newsEditDTO){
        List<AnnRepoNews> list = annRepoNewsMapper.getAllNews(newsEditDTO);
        return ResultWrapper.success(SUCCESS,list);
    }
    public ResultWrapper<String> deleteNews(long id){
        AnnRepoNews annRepoNews = annRepoNewsMapper.selectById(id);
        if(annRepoNews == null){
            throw new BusinessException(ANN_REPO_NEWS_NOT_EXIST);
        }
        annRepoNewsMapper.deleteNews(id);
        return ResultWrapper.success(SUCCESS);
    }

    public ResultWrapper<String> updateStatus(long id,int status){
        AnnRepoNews annRepoNews = annRepoNewsMapper.selectById(id);
        if(annRepoNews == null){
            throw new BusinessException(ANN_REPO_NEWS_NOT_EXIST);
        }
        annRepoNewsMapper.updateStatus(id,status);
        return ResultWrapper.success(SUCCESS);
    }

    public ResultWrapper<String> updatePrivace(long id,int privace){
        AnnRepoNews annRepoNews = annRepoNewsMapper.selectById(id);
        if(annRepoNews == null){
            throw new BusinessException(ANN_REPO_NEWS_NOT_EXIST);
        }
        annRepoNewsMapper.updatePrivace(id,privace);
        return ResultWrapper.success(SUCCESS);
    }
}




